﻿Imports System.Data.SqlClient
Imports System.Web.Security
Imports System.Security.Cryptography

Partial Class admin_registerAdmin
    Inherits System.Web.UI.Page

    Protected Sub btnAddCredentials_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddCredentials.Click

        Dim conn As SqlConnection
        Dim comm As SqlCommand
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("Henson Family").ConnectionString
        Dim objRng As New RNGCryptoServiceProvider
        Dim intSaltSize As Integer = 16

        Dim objByte() As Byte = New Byte(intSaltSize) {}
        objRng.GetBytes(objByte)
        Dim strSalt As String = Convert.ToBase64String(objByte)

        conn = New SqlConnection(connectionString)
        comm = New SqlCommand("INSERT INTO Users (FName, LName, BirthDate, Email, Password, Salt, Private) VALUES (@FirstName, @LastName, @BirthDate, @Email, @Password, @Salt, @Private)", conn)

        comm.Parameters.Add("@FirstName", Data.SqlDbType.NVarChar)
        comm.Parameters("@FirstName").Value = txtFName.Text
        comm.Parameters.Add("@LastName", Data.SqlDbType.NVarChar)
        comm.Parameters("@LastName").Value = txtLName.Text
        comm.Parameters.Add("@BirthDate", Data.SqlDbType.NVarChar)
        comm.Parameters("@BirthDate").Value = txtBirthDate.Text
        comm.Parameters.Add("@Email", Data.SqlDbType.NVarChar)
        comm.Parameters("@Email").Value = txtEmail.Text
        comm.Parameters.Add("@Password", Data.SqlDbType.NVarChar)
        comm.Parameters("@Password").Value = FormsAuthentication.HashPasswordForStoringInConfigFile(strSalt + txtPassword.Text, "SHA1")
        comm.Parameters.Add("@Salt", Data.SqlDbType.NVarChar)
        comm.Parameters("@Salt").Value = strSalt
        comm.Parameters.Add("@Private", Data.SqlDbType.Bit)
        comm.Parameters("@Private").Value = "False"

        conn.Open()
        comm.ExecuteNonQuery()
        conn.Close()

    End Sub
End Class
